package com.example.competionmanagebackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.competionmanagebackend.entity.Competitions;
import com.example.competionmanagebackend.service.CompetitionsService;
import com.example.competionmanagebackend.utils.PageRequest;
import com.example.competionmanagebackend.utils.PageResult;
import com.example.competionmanagebackend.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/competitions")
public class CompetitionsController {

    @Autowired
    private CompetitionsService competitionsService;

    /**
     * 分页查询竞赛
     */
    @PostMapping("/getCompetitionPage")
    public ResponseEntity<Result<PageResult<Competitions>>> getCompetitionPage(
            @RequestBody Map<String, Object> params) {
        Integer currentPage = (Integer) params.get("currentPage");
        Integer pageSize = (Integer) params.get("pageSize");
        Competitions query = new Competitions();

        if (params.containsKey("id") && params.get("id") != null && params.get("id") != "") {
            query.setId(Integer.valueOf(params.get("id").toString()));
        }
        if (params.containsKey("competitionName") && params.get("competitionName") != null) {
            query.setCompetitionName(params.get("competitionName").toString());
        }
        if (params.containsKey("startDate") && params.get("startDate") != null) {
            String startDateStr = params.get("startDate").toString();
            query.setStartDate(parseDate(startDateStr));
        }
        if (params.containsKey("endDate") && params.get("endDate") != null) {
            String endDateStr = params.get("endDate").toString();
            query.setEndDate(parseDate(endDateStr));
        }

        Page<Competitions> page = competitionsService.getCompetitionsPage(currentPage, pageSize, query);
        PageResult<Competitions> result = new PageResult<>(page);
        return Result.success(result);
    }

    // 辅助方法：解析日期字符串
    private Date parseDate(String dateStr) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.parse(dateStr);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取所有竞赛
     */
    @GetMapping("/all")
    public ResponseEntity<Result<List<Competitions>>> getAllCompetitions() {
        List<Competitions> competitions = competitionsService.getAllCompetitions();
        return Result.success(competitions);
    }

    /**
     * 新增或修改竞赛信息
     */
    @PostMapping("/edit")
    public ResponseEntity<Result<Competitions>> update(@RequestBody Competitions competition) {
        if (competition.getId() == null) {
            competitionsService.insertCompetition(competition);
        } else {
            competitionsService.updateCompetition(competition);
        }
        return Result.success(competition);
    }

    /**
     * 删除竞赛信息
     */
    @GetMapping("/delete")
    public ResponseEntity<Result<String>> deleteCompetition(@RequestParam("id") Integer id) {
        competitionsService.deleteCompetition(id);
        return Result.success("删除成功");
    }
}